package bdz.projects.lavender.opsm.main
{
	
	import bdz.common.model.XMLLoader;
	import bdz.common.events.XMLLoaderEvent;
	import bdz.projects.lavender.opsm.ui.MaskedImageStrip;
	import flash.display.*;
	import flash.events.*;
	import bdz.projects.lavender.opsm.model.HomeItem;
	
	/**
	 * ...
	 * @author ...
	 */
	public class OPSMHomeStrip extends Sprite {
		private var bg:MovieClip;
		private var istrip:MaskedImageStrip;
		private var xloader:XMLLoader;
		private var preloader:MovieClip;
		
		function OPSMHomeStrip() {
			addEventListener(Event.ADDED_TO_STAGE, initializeComponent);
		}
		private function initializeComponent(evt:Event):void {
			createElements();
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.addEventListener(Event.RESIZE, onStageResize);
			
			onStageResize();	
		}
		private function onStageResize(evt:Event = null):void {
			bg.width = stage.stageWidth;
		}
		private function onXMLLoaded(evt:XMLLoaderEvent):void {
			var xlist:XMLList = new XML(evt.data).homePromo;
			var props:Array = ["title", "type", "url", "short", "image"];
			var arr:Array = new Array();
			for each(var xm:XML in xlist) {
				var obj:HomeItem = new HomeItem();
				for each(var prop:String in props) {
					obj[prop] = xm[prop].toString();
				}
				arr.push(obj);
			}
			try {
				removeChild(preloader);
			}catch(e:Error){}
			istrip.contents = arr;
		}
		private function createElements():void {
			bg = new StripBG();
			addChild(bg);
			istrip = new MaskedImageStrip();
			addChild(istrip);
			xloader = new XMLLoader("onXMLLoaded");
			xloader.addEventListener("onXMLLoaded", onXMLLoaded);
			xloader.location = loaderInfo.parameters.source || "opsm_home_test.xml";
			xloader.send();
			createPreloader();
		}
		private function createPreloader():void {
			preloader = new PreloadClip();
			preloader.x = Math.ceil(stage.stageWidth / 2);
			preloader.y = Math.ceil(stage.stageHeight / 2);
			addChild(preloader);
		}
	}
	
	
}
